#!/bin/bash

# 前提是先安装protoc, 安装下载地址：https://github.com/protocolbuffers/protobuf/releases
# 再安装下面这2个go插件
#go install google.golang.org/protobuf/cmd/protoc-gen-go
#go get google.golang.org/grpc/cmd/protoc-gen-go-grpc
#go install google.golang.org/grpc/cmd/protoc-gen-go-grpc

modName="gitee.com/captials-team/ubdframe/src/protobufs/pb/common"

# 这是示例代码
#protoc --proto_path=./protos/pb_structs/ \
#  --go_out=. --go-grpc_out=. \
#  ./protos/pb_structs/*.proto
#
#protoc --proto_path=./protos/ --go_out=. \
#  --go_opt=Mpb_structs/demo.proto=${modName} \
#  --go_opt=Mpb_structs/demo2.proto=${modName} \
#  --go_opt=Mpb_structs/error.proto=${modName} \
#  --go-grpc_opt=Mpb_structs/demo.proto=${modName} \
#  --go-grpc_opt=Mpb_structs/demo2.proto=${modName} \
#  --go-grpc_opt=Mpb_structs/admin.proto=${modName} \
#  --go-grpc_opt=Mpb_structs/error.proto=${modName} \
#  --go-grpc_out=. \
#  ./protos/*.proto

files=$(find ./protos -type f)
stsFiles=$(find ./protos/common -type f -exec basename {} \;)  #只显示文件名

protoc --proto_path=./protos/common/ \
    --go_out=. --go-grpc_out=. \
    ./protos/common/*.proto

#需要手动拼接 common（公共结构体）的文件
s=""
for n in ${stsFiles[*]}
do
  echo "common ===> $n"
  s="$s --go_opt=Mcommon/$n=${modName} --go-grpc_opt=Mcommon/$n=${modName}"
done

echo "$s"

#protoc输出
for n in ${files[*]}
do
  echo "===> $n"
  lineOpt=""

  hasImport=$(cat $n | grep "^import ")
  if [ "$hasImport" != "" ] ;then
    lineOpt="$s"
  fi

  protoc --proto_path=./protos/ \
    ${lineOpt} \
    --go_out=. --go-grpc_out=. \
    $n
done